<include file="public@header"/>
</head>
<body>
<div class="wrap js-check-wrap">
	<ul class="nav nav-tabs">
		<li><a href="{:url('AdminMeeting/address')}">会议地址列表</a></li>
        <li class="active"><a href="{:url('AdminMeeting/addressForm')}">添加地址</a></li> 
	</ul>
	<form method="post" class="form-horizontal js-ajax-form margin-top-20" action="{:url('AdminMeeting/addressForm')}" onsubmit="submitTest();">
		<div class="form-group">
			<label class="col-sm-2 control-label"><span class="form-required">*</span>所在地区:</label>
			<div class="col-md-2 col-sm-10">
				<select id="regionOneId" name="provinceId" class="form-control" onchange="loadSelect('one')">
					<option value="" >--省级--</option>
					<foreach name="$regionList['oneRegion']" item="one">
						<option value="{$one.region_id}" <?php echo isset($regionList['one']) && intval($regionList['one'])==$one['region_id'] ? 'selected="selected"' : ''; ?>>{$one.region_name}</option>
					</foreach>	
				</select>
			</div>
			<div class="col-md-2 col-sm-10">
				<select id="regionTwoId" name="cityId" class="form-control" onchange="loadSelect('two')">
					<option value="" >--市级--</option>
					<if condition="!empty($regionList['twoRegion'][$regionList['one']])">
						<foreach name="$regionList['twoRegion'][$regionList['one']]" item="two">
							<option value="{$two.region_id}" <?php echo isset($regionList['two']) && intval($regionList['two'])==$two['region_id'] ? 'selected="selected"' : ''; ?>>{$two.region_name}</option>
						</foreach>
					</if>
				</select>
			</div>
			<div class="col-md-2 col-sm-10">
				<select id="regionThreeId" name="areaId" class="form-control">
					<option value="" >--地区--</option>
					<if condition="!empty($regionList['threeRegion'][$regionList['two']])">
						<foreach name="$regionList['threeRegion'][$regionList['two']]" item="three">
							<option value="{$three.region_id}" <?php echo isset($regionList['three']) && intval($regionList['three'])==$three['region_id'] ? 'selected="selected"' : ''; ?>>{$three.region_name}</option>
						</foreach>
					</if>
				</select>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label"><span class="form-required">*</span>详细地址:</label>
			<div class="col-md-6 col-sm-10">
				<input type="text" class="form-control" name="address" value="{$data.address|default=''}" placeholder="请输入详细地址">
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label"><span class="form-required">*</span>坐标：</label>
			<div class="col-md-2 col-sm-10">
				<input type="text" class="form-control" name="lng" value="{$data.lng|default=''}"  placeholder="经度" readonly>
			</div>
			<div class="col-md-2 col-sm-10">
				<input type="text" class="form-control" name="lat" value="{$data.lat|default=''}"  placeholder="纬度" readonly>
			</div>
			<div class="col-md-2 col-sm-10">
				<a class="btn btn-info" onclick="getLatLng()">获取坐标</a>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label">默认地址:</label>
			<div class="col-md-1 col-sm-10">
				是：&nbsp;<input type="radio" name="is_default" value="1" <?php echo isset($data['is_default']) && $data['is_default'] == 1 ? 'checked' : ''; ?>>
			</div>
			<div class="col-md-1 col-sm-10">
				否：&nbsp;<input type="radio" name="is_default" value="0" <?php echo empty($data['is_default']) && $data['is_default'] == 0 ? 'checked' : ''; ?>>
			</div>
		</div>
		
		<div class="col-sm-offset-2 col-sm-10">
			<button type="button" class="btn btn-primary" id="js-ajax-submit">{:lang("SAVE")}</button>
			<a class="btn btn-default" href="{:url('AdminMeeting/address')}">返回</a>
		</div>
	</form>

</div>
<style type="text/css">
#container{
    min-width:500px;
    min-height:400px;
}
</style>
<div id="container"></div>
<script src="__STATIC__/js/admin.js"></script>
<script>
	$('#js-ajax-submit').on('click', function () {
		var data = {
			id:"{$data['id']|default=''}",
			provinceId:$("#regionOneId option:selected").val(),
			cityId:$("#regionTwoId option:selected").val(),
			areaId:$("#regionThreeId option:selected").val(),
			lat:$("input[name='lat']").val(),
			lng:$("input[name='lng']").val(),
			address:$("input[name='address']").val(),
			is_default:$("input[name='is_default']:checked").val(),
		}
        $.ajax({
		        url: "{:url('AdminMeeting/addressForm')}",
		        dataType: 'json',
		        data: data,
		        type: "POST",
		        success: function(data){
					if (data.code == '1') {
		                noty({
		                    text: data.msg,
		                    type: 'success',
		                    layout: 'topCenter',
		                    modal: true,
		                    timeout: 800,
		                    callback: {
		                        afterClose: function () {
	                                if (data.url) {
	                                    //返回带跳转地址
	                                    window.location.href = data.url;
	                                    // history.go(0) // 当前页面刷新
	                                } else {
	                                    //刷新当前页
	                                    reloadPage(window);
	                                }
		                        }
		                    }
		                }).show();
		            } else if (data.code == '0') {
		                art.dialog({
		                    content: data.msg,
		                    icon: 'warning',
		                    title: '提示',
		                    ok: function () {
		                        this.title(data.msg);
		                        return true;
		                    }
		                });
		            }
		        }
	        });
    });
	// 地址搜索
	function loadSelect(type){
		if(type == 'one'){
			var id = $("#regionOneId option:selected").val();
            if(!id){
	               $('#regionTwoId').empty();
	               $('#regionTwoId').append("<option value=>请选择..</option>");
	               $('#regionThreeId').hide();return;
			}
		}

		if(type == 'two'){
			var id = $("#regionTwoId option:selected").val();
			if(!id){
				$('#regionThreeId').hide();return;
			}
		}
		              
		$.ajax({
	        url: "{:url('AdminMeeting/getSelect')}",
	        dataType: 'json',
	        data: {"parentid": id},
	        type: "POST",
	        success: function(res){
				data = res.data;
              if(type == 'one'){
                  $('#regionTwoId').empty();
                  $('#regionTwoId').append("<option value=>请选择..</option>");
                  $('#regionThreeId').empty();
                  $('#regionThreeId').append("<option value=>请选择..</option>");
                  if($("#regionOneId option:selected").val()){
                      for(var i=0;i<(data.length);i++){
                          $('#regionTwoId').append("<option value='"+data[i]['region_id']+"'>"+data[i]['region_name']+"</option>");
                      }
                  }
                  $('#regionThreeId').hide();
              }
              if(type == 'two'){
                  $('#regionThreeId').empty();
                  $('#regionThreeId').append("<option value=>请选择..</option>");
                  for(var i=0;i<(data.length);i++){
                      $('#regionThreeId').append("<option value='"+data[i]['region_id']+"'>"+data[i]['region_name']+"</option>");
                  }
                  if(data.length == 0){
                      $('#regionThreeId').hide();
                  }else{
                      $('#regionThreeId').show();
                  }
              }
	        }
        });
	}

	
</script>

<script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp"></script>
<script>
	var address = '{$address}'
	window.onload = function(){
		if ( address != '' && address != null ) mapShow(address)
	}
	// 根据地址获取定位
	function getLatLng(){
		var data = {
			provinceId:$("#regionOneId option:selected").val(),
			cityId:$("#regionTwoId option:selected").val(),
			areaId:$("#regionThreeId option:selected").val(),
			address:$("input[name='address']").val(),
		}
		$.ajax({
	        url: "{:url('AdminMeeting/getLatLng')}",
	        dataType: 'json',
	        data: data,
	        type: "POST",
	        success: function(data){
				if (data.code == '1') {
	               mapShow(data.msg)
	            } else if (data.code == '0') {
	                art.dialog({
	                    content: data.msg,
	                    icon: 'warning',
	                    ok: function () {
	                        this.title(data.msg);
	                        return true;
	                    }
	                });
	            }
	        }
        });
	}

	// 根据地址获取经纬调出地图
	function mapShow(address)
	{
		var geo = new qq.maps.Geocoder()
		// console.log(geo)
		geo.getLocation(address)//地址
		geo.setComplete(function(res){
		    // console.log(res,res.detail.location)//得到经纬度
		    $("input[name='lat']").val(res.detail.location.lat)
		    $("input[name='lng']").val(res.detail.location.lng)
		    var map = new qq.maps.Map(document.getElementById('container'),{
		        center: res.detail.location,//将经纬度加到center属性上
		        zoom: 16,//缩放   
		        draggable: true,//是否可拖拽
		        scrollwheel: true,//是否可滚动缩放
		        disableDoubleClickZoom: false
		    })
		    var marker = new qq.maps.Marker({
		        position: res.detail.location,//标记的经纬度
		        animation: qq.maps.MarkerAnimation.BOUNCE,//标记的动画
		        map: map//标记的地图
		    })

		    var citylocation = new qq.maps.CityService({
		        //设置地图
		        map : map,
		        complete : function(results){
		            console.log(9999,results)
		        }
		    });
		})
	}
</script>
</body>
</html>